package com.example.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.ChooseCurriculum;
import com.example.entity.Curriculum;

import java.util.ArrayList;
import java.util.List;

public class CurriculumMessage extends SQLiteOpenHelper {
    private static final String DB_NAME = "schools" ;
    private static final int DB_Version = 1 ;
    public Curriculum getCurriculumById;

    public CurriculumMessage(@Nullable Context context) {
        super(context, DB_NAME, null, DB_Version);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE curriculums (" +
                "curriculumName TEXT NOT NULL ," +
                "curriculumId TEXT PRIMARY KEY ," +
                "curriculumTeacher TEXT NOT NULL ," +
                "curriculumClass TEXT ," +
                "curriculumTime INT )" ;
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS curriculums");
        onCreate(db);
    }

    public long addCurriculum(Curriculum curriculum){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("curriculumName" ,curriculum.getCurriculumName());
        contentValues.put("curriculumId" , curriculum.getCurriculumId());
        contentValues.put("curriculumTeacher" , curriculum.getCurriculumTeacher());
        contentValues.put("curriculumClass" , curriculum.getCurriculumClass());
        contentValues.put("curriculumTime" , curriculum.getCurriculumTime());
        long curriculums;
        curriculums = db.insert("curriculums" , null , contentValues);
        return curriculums;
    }

    //查询所有课程
    @SuppressLint("Range")
    public List<Curriculum> getAllCurriculum(){
        List<Curriculum> curriculums = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query("curriculums" , new String[]{"curriculumName" , "curriculumId" ,
                "curriculumTeacher" , "curriculumClass" , "curriculumTime" },
                null , null , null , null , null);
        if (cursor != null && cursor.moveToFirst()){
            do {
                String curriculumName = cursor.getString(cursor.getColumnIndex("curriculumName"));
                String curriculumId = cursor.getString(cursor.getColumnIndex("curriculumId"));
                String curriculumTeacher = cursor.getString(cursor.getColumnIndex("curriculumTeacher"));
                String curriculumClass = cursor.getString(cursor.getColumnIndex("curriculumClass"));
                int curriculumTime = Integer.parseInt(cursor.getString(cursor.getColumnIndex("curriculumTime")));
                curriculums.add(new Curriculum(curriculumName , curriculumId , curriculumTeacher , curriculumClass , curriculumTime));
            }while (cursor.moveToNext());
        }
        if (cursor != null){
            cursor.close();
        }
        db.close();
        return curriculums ;
    }

    // 根据curriculumId获取curriculumName
    public String getCourseName(String curriculumId) {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = { "curriculumName" };
        String selection = "curriculumId = ?";
        String[] selectionArgs = { curriculumId };
        String curriculumName = null;
        Cursor cursor = null;
        try {
            cursor = db.query("curriculums", columns, selection, selectionArgs, null, null, null);
            if (cursor.moveToFirst()) {
                curriculumName = cursor.getString(0); // 索引0对应curriculumName
            }
        } catch (Exception e) {
            // 异常处理
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return curriculumName;
    }

    @SuppressLint("Range")
    public Curriculum getCourseDetails(String curriculumId) {
        SQLiteDatabase db = this.getReadableDatabase();
        Curriculum course = null;
        String[] columns = { "curriculumId", "curriculumName", "curriculumTeacher", "curriculumClass", "curriculumTime" };
        String selection = "curriculumId = ?";
        String[] selectionArgs = { curriculumId };
        Cursor cursor = null;
        try {
            cursor = db.query("curriculums", columns, selection, selectionArgs, null, null, null);
            if (cursor.moveToFirst()) {
                // 假设Curriculum是一个实体类，用于封装课程信息
                course = new Curriculum(
                        cursor.getString(cursor.getColumnIndex("curriculumId")),
                        cursor.getString(cursor.getColumnIndex("curriculumName")),
                        cursor.getString(cursor.getColumnIndex("curriculumTeacher")),
                        cursor.getString(cursor.getColumnIndex("curriculumClass")),
                        cursor.getInt(cursor.getColumnIndex("curriculumTime"))
                );
            }
        } catch (Exception e) {
            // 异常处理
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            db.close();
        }
        return course;
    }
}
